//
//  MovieViewController.m
//  卢卡的时光网
//
//  Created by 卡神 on 15/7/17.
//  Copyright © 2015年 lok. All rights reserved.
//

#import "MovieViewController.h"
#import "MovieCell.h"
#import "common.h"
 

@interface MovieViewController () {
    BOOL _isLeft;
    NSMutableArray *_models;
}

@end

@implementation MovieViewController



#pragma mark -createViews

- (void)_createRightBarButton {
    UIView *flipView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"list_home"] forState:UIControlStateNormal];
    [button sizeToFit];
    [button addTarget:self action:@selector(flip:) forControlEvents:UIControlEventTouchUpInside];
    [flipView addSubview:button];
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithCustomView: flipView];
    self.navigationItem.rightBarButtonItem = right;
}



- (void)_createPosterView {
    _posterView = [[PosterView alloc] initWithFrame:self.view.bounds];
    _posterView.imageData = _models;
    [self.view addSubview:_posterView];
}

- (void)_createMovieListView {
    _movieListView = [[UITableView alloc] initWithFrame: CGRectMake(0, 0, kWidth, kHeight)];
    _movieListView.delegate = self;
    _movieListView.dataSource = self;
    [_movieListView setBackgroundColor:[UIColor grayColor]];
    UINib *nib = [UINib nibWithNibName:@"MovieCell" bundle:[NSBundle mainBundle]];
    [_movieListView registerNib:nib forCellReuseIdentifier:@"myCell"];
    [self.view addSubview:_movieListView];
}

 

#pragma mark -button's Action

- (void)flip: (UIButton *)button {
    UIView *view = self.navigationItem.rightBarButtonItem.customView;
   
    UIViewAnimationOptions option = _isLeft ? UIViewAnimationOptionTransitionFlipFromRight : UIViewAnimationOptionTransitionFlipFromLeft;
//    [UIView beginAnimations:@"animation" context:nil];
//    [UIView setAnimationDuration:0.5];
//    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view cache:YES];
    [UIView transitionWithView:view duration:0.5 options:option animations:nil completion:nil];
     [UIView transitionWithView:self.view duration:0.5 options:option animations:nil completion:nil];
    if (_isLeft) {
        [button setImage:[UIImage imageNamed:@"list_home"] forState:UIControlStateNormal];
    } else {
        [button setImage:[UIImage imageNamed:@"poster_home"] forState:UIControlStateNormal];
    }
    _movieListView.hidden = !_movieListView.hidden;
    _posterView.hidden = !_movieListView.hidden;
    _isLeft = !_isLeft;
}

//- (void)_flipView: (UIView *)view withIsLeft: (BOOL)isLeft {
//    UIViewAnimationTransition option = isLeft ? UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight;
//    [UIView transitionWithView:view duration:0.5 options:(UIViewAnimationOptions)option animations:nil completion:nil];
//}
#pragma mark -lifeCycle

- (void)viewDidLoad {
    _models = [[NSMutableArray alloc] init];
    [super viewDidLoad];
    [self _createRightBarButton];
    [self _createMovieListView];
    [self _createPosterView];
    [self _loadData];
    _movieListView.hidden = YES;
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (nonnull instancetype)initWithNibName:(nullable NSString *)nibNameOrNil bundle:(nullable NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.title = @"电影";
    }
    return self;
}

#pragma mark -TableView's dataSourse and delegate

- (CGFloat)tableView:(nonnull UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    return 115;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_models count];
}

- (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    MovieCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath];
    cell.model = _models[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)_loadData {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"us_box" ofType:@"json"];
    NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:path] options:NSJSONReadingMutableContainers error:nil];
    NSArray *dataArray = [dataDic objectForKey:@"subjects"];
    for (NSDictionary *dic in dataArray) {
        NSDictionary *subDic = [dic objectForKey:@"subject"];
        MoiveInformation *model = [[MoiveInformation alloc] init];
        model.title = [subDic objectForKey:@"title"];
        model.year = [subDic objectForKey:@"year"];
        model.images = [subDic objectForKey:@"images"];
        model.average = [[[subDic objectForKey:@"rating"] objectForKey:@"average"] doubleValue];
        model.english = [subDic valueForKey:@"original_title"];
        [_models addObject:model];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
